package com.arpa.wms.controller;

import com.arpa.core.web.MediaTypes;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.common.util.ShipmentWarehouseValidate;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.wms.domain.consts.CommonConst;
import com.arpa.wms.domain.dto.PutawayDTO;
import com.arpa.wms.domain.entity.Inbound;
import com.arpa.wms.domain.entity.Putaway;
import com.arpa.wms.domain.vo.PutawayVO;
import com.arpa.wms.service.IPutawayService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 上架 Controller
 * </p>
 *
 * @author LEO
 * @since 2020-08-04
 */
@Api(tags = "上架")
@RestController
@RequestMapping(value = "/putaway", produces = "application/json;charset=UTF-8")
@Log4j2
public class PutawayController {

    private final IPutawayService iPutawayService;


    public PutawayController(IPutawayService iPutawayService) {
        this.iPutawayService = iPutawayService;

    }

    /**
     * 获取 上架 列表
     *
     * @param putawayDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "获取 上架 列表", notes = "获取 上架 列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = PutawayVO.class))
    @NeedLogin
    @GetMapping()
    public Result list(PutawayDTO putawayDTO, HttpServletRequest request) {
        try {
            //校验授权仓库code和货主code不能为空
            Map<String, List<String>> authMap = ShipmentWarehouseValidate.validateShipmentCodesAndWarehouseCodes();
            //登录账号所在组织机构过滤
            putawayDTO.setGroupCode(UserUtil.getBranchCode());
            //登录账号授权的仓库过滤
            putawayDTO.setWarehouseCodes(authMap.get(CommonConst.WAREHOUSE_CODES));
            //登录仓库授权的货主过滤
            putawayDTO.setShipmentCodes(authMap.get(CommonConst.SHIPMENT_CODES));

            PutawayVO putawayVOSum = iPutawayService.queryListTotal(putawayDTO);
            List<PutawayVO> putawayVOList = iPutawayService.queryList(putawayDTO);

            Page returnPage = new Page(putawayDTO.getPageNum(), putawayDTO.getPageSize());
            returnPage.setRecords(putawayVOList);
            returnPage.setTotal(putawayVOSum.getTotal());
            returnPage.setSumObject(putawayVOSum);
            return Result.ok().data(returnPage);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE, e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 添加 上架
     *
     * @param entity  Putaway
     * @param request
     * @return
     */
    @ApiOperation(value = "添加 上架 ", notes = "添加 上架 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PostMapping()
    public Result create(Putaway entity, HttpServletRequest request) {
        try {
            entity.setGroupCode(UserUtil.getBranchCode());
            iPutawayService.save(entity);
            return Result.ok("添加成功");
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE, e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取上架详情
     *
     * @param code
     * @param request
     * @return
     */
    @ApiOperation(value = "获取上架详情", notes = "获取上架详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
            @ApiImplicitParam(name = "code", value = "上架code", paramType = "path", dataType = "String")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = Putaway.class))
    @NeedLogin
    @GetMapping(value = "/{code}")
    public Result putaway(@PathVariable String code,HttpServletRequest request) {
        try {
            PutawayVO putawayVO = iPutawayService.getVOByCode(code);
            return Result.ok().data(putawayVO);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE, e);
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改上架
     *
     * @param entity  上架
     * @param request
     * @returnda
     */
    @ApiOperation(value = "修改上架", notes = "修改上架")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PutMapping()
    public Result update(Putaway entity,HttpServletRequest request) {
        if (StringUtils.isBlank(entity.getCode())) {
            return Result.error("code不能为空");
        }
        int updateNum = iPutawayService.updateByCode(entity);
        if (updateNum == 0) {
            return Result.error("没有更新数据！code:" + entity.getCode());
        } else {
            return Result.ok("修改成功,更新" + updateNum + "条数据！");
        }
    }

    /**
     * 删除 上架
     *
     * @param codes
     * @param request
     * @return
     */
    @ApiOperation(value = "删除 上架 ", notes = "删除 上架 ", produces = MediaTypes.JSON_UTF_8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
            @ApiImplicitParam(name = "codes", value = "codes", required = true, paramType = "query", dataType = "List")
    })
    @NeedLogin
    @DeleteMapping(value = "")
    public Result batchdel(@RequestParam List<String> codes, HttpServletRequest request) {
        try {
            iPutawayService.remove(new QueryWrapper<Putaway>().lambda().in(Putaway::getCode, codes).eq(Putaway::getGroupCode,UserUtil.getBranchCode()));
            return Result.ok();
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE, e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 上架保存
     *
     * @param entity
     * @param request
     * @return
     */
    @ApiOperation(value = "保存 上架 ", notes = "保存 上架 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PostMapping("/save")
    public Result save(@RequestBody @Validated(Putaway.Save.class) Putaway entity, HttpServletRequest request) {
        try {
            return iPutawayService.localSave(entity);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE, e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 上架确认
     *
     * @param entity
     * @return
     */
    @ApiOperation(value = "确认 上架 ", notes = "确认 上架 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
            @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PostMapping("/check")
    public Result check(@RequestBody @Validated(Putaway.Save.class) Putaway entity) {
        try {
            Inbound inbound= iPutawayService.check(entity);

            //推送奇门入库单确认信息,"0"-普通入库单 "1"-退货入库单
            iPutawayService.orderConfirm(inbound);

            return Result.ok("上架成功");
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE, e);
            return Result.error(e.getMessage());
        }
    }
}
